package com.ruoyi.bizsys.domain;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.domain.BaseEntity;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 贷款借据对象 loan_iou
 *
 * @author african-loan
 * @date 2021-12-30
 */

@Data
public class LoanIou extends BaseEntity {
    private static final long serialVersionUID = 1L;

    public static final int LOAN_IOU_STATUS_NORMAL = 0;//待还款
    public static final int LOAN_IOU_STATUS_SETTLE = 1;//结清
    public static final int LOAN_IOU_STATUS_OVERDUE = 2;//逾期

    /*** 贷款日期 */
    @TableField(exist = false)
    private String loanDate;
    /**利息*/
    @TableField(exist = false)
    private BigDecimal interest;
    /*** 服务费率*/
    @TableField(exist = false)
    private BigDecimal serverFeeRate;
    /***当天距离贷款多少天*/
    @TableField(exist = false)
    private Integer nowDayIndex;

    // 以上非数据库字段  wqg 使用，已经排除数据库映射

    /**
     * ID
     */
    @Excel(name = "ID", readConverterExp = "ID")
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 贷款流水号
     */
    @Excel(name = "贷款流水号")
    private String loanSerialNo;
    /** 子借据号 */
    @Excel(name = "子借据号")
    private Long loanSerialDeferredNo;
    /** 0无子1有子借据 */
    @Excel(name = "0无子1有子借据")
    private Integer isDeferred;

    /** 延期的天数 */
    @Excel(name = "延期的天数")
    private Integer deferredPeriodNum;

    /** 延期的还款日期 */
    @Excel(name = "延期的还款日期")
    private String deferredRepayDate;

    /** 延期产品等级对应customer_info-credit_rating */
    @Excel(name = "延期产品等级对应customer_info-credit_rating")
    private String creditRating;

    private Long productId;
    /**
     * 客户号
     */
    @Excel(name = "客户号")
    private String custNo;

    /**
     * 借款金额
     */
    @Excel(name = "贷款金额")
    private BigDecimal loanAmt;

    /**
     * 趸交
     */
    @Excel(name = "趸交")
    private String isSinglePayment;

    /**
     * 期数
     */
    @Excel(name = "期数")
    private Integer periodNum;

    /**
     * 单位
     */
    @Excel(name = "单位")
    private String unit;

    /**
     * 利(息)率
     */
    @Excel(name = "利率")
    private BigDecimal interestRate;

    /**
     * 滞纳金日费率
     */
    @Excel(name = "滞纳金日费率")
    private BigDecimal dateLateFee;

    @TableField(value = "show_interest_rate")
    @Excel(name = "产品展示利率")
    private BigDecimal showInterestRate;

    @TableField(value = "show_service_rate")
    @Excel(name = "展示服务费率")
    private BigDecimal showServiceRate;

    /**
     * 滞纳金按日收费宽限期（天数）
     */
    @Excel(name = "滞纳金按日收费宽限期", readConverterExp = "天=数")
    private Integer lateFeeGracePeriod;

    /**
     * 滞纳金计算最大天数
     */
    @Excel(name = "滞纳金计算最大天数")
    private Integer lateFeeMaxDays;

    /**
     * 放款本金
     */
    @Excel(name = "放款本金")
    private BigDecimal loanPrincipal;

    /**
     * 已还本金
     */
    @Excel(name = "已还本金")
    private BigDecimal repaidPrincipal;

    /**
     * 还款日期
     */
    @Excel(name = "还款日期")
    private String repayDate;

    /** 服务费费率 */
    @Excel(name = "服务费费率")
    private BigDecimal serviceRate;

    /**
     * 服务费
     */
    @Excel(name = "服务费")
    private BigDecimal serviceFee;

    /**
     * 已还服务费
     */
    @Excel(name = "已还服务费")
    private BigDecimal repaidServiceFee;

    /**
     * 总利息
     */
    @Excel(name = "总利息")
    private BigDecimal loanInterest;

    /**
     * 已还总利息
     */
    @Excel(name = "已还总利息")
    private BigDecimal repaidInterest;

    /**
     * 减免金额
     */
    @Excel(name = "系统减免金额")
    private BigDecimal feeRemissionAmt;

    /**
     * 滞纳金
     */
    @Excel(name = "滞纳金")
    private BigDecimal lateFee;

    /**
     * 已还滞纳金
     */
    @Excel(name = "已还滞纳金")
    private BigDecimal repaidLateFee;

    /**
     * 逾期金额
     */
    @Excel(name = "逾期金额")
    private BigDecimal overdueAmount;

    /**
     * 借据状态
     */
    @Excel(name = "借据状态")
    private Integer iouStatus;

    /**
     * 结清时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "结清时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    private Date settleDate;

    /***减免优惠金额：Quick repay场景（其他场景请注释说明）*/
    private BigDecimal reduceAmt;
    /**
     * 状态
     */

    @Excel(name = "状态")
    private String state;


    /**
     * 备注
     */

    @Excel(name = "备注")
    private String remark;


    /**
     * 创建者
     */

    @Excel(name = "创建者")
    private String createBy;


    /**
     * 创建时间
     */

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "创建时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;


    /**
     * 更新者
     */

    @Excel(name = "更新者")
    private String updateBy;

    /**
     * 财务回溯次数
     */
    private Integer numFinance;


    /**
     * 更新时间
     */

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "更新时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    /**
     * 计算 OverdueAmount wqg
     * @param loanIou
     * @return
     */
    public static BigDecimal calculationOverdueAmount(LoanIou loanIou) {
        return loanIou.getLoanPrincipal().add(loanIou.getServiceFee()).add(loanIou.getLoanInterest()).add(loanIou.getLateFee())
                .subtract(loanIou.getRepaidPrincipal()==null?BigDecimal.ZERO:loanIou.getRepaidPrincipal())
                .subtract(loanIou.getRepaidInterest()==null?BigDecimal.ZERO:loanIou.getRepaidInterest())
                .subtract(loanIou.getRepaidServiceFee()==null?BigDecimal.ZERO:loanIou.getRepaidServiceFee())
                .subtract(loanIou.getRepaidLateFee()==null?BigDecimal.ZERO:loanIou.getRepaidLateFee())
                .subtract(loanIou.getReduceAmt()==null?BigDecimal.ZERO:loanIou.getReduceAmt())
                .subtract(loanIou.getFeeRemissionAmt()==null?BigDecimal.ZERO:loanIou.getFeeRemissionAmt());
    }
}